//
//  MSDIYDetailVC.m
//  MS
//
//  Created by Yuan on 13-4-2.
//  Copyright (c) 2013年 He Yuan. All rights reserved.
//

#import "MSDIYDetailVC.h"
#import "WaterFlowView.h"
#import "ImageViewCell.h"
#import "UIImageView+WebCache.h"

@interface MSDIYDetailVC ()<WaterFlowViewDelegate,WaterFlowViewDataSource,UIScrollViewDelegate>
{
    WaterFlowView *waterFlow;
    NSMutableArray *arrayData;
}
@property(nonatomic,retain) WaterFlowView *waterFlow;

@property(nonatomic,retain) NSMutableArray *arrayData;


//- (void)dataSourceDidLoad;
//
//- (void)dataSourceDidError;

@end

@implementation MSDIYDetailVC
@synthesize waterFlow = _waterFlow;
@synthesize arrayData = _arrayData;

- (id)initWithInfo:(NSDictionary *)info
{
    self = [super init];
    if (self)
    {
        self.title = [info objectForKey:@"title"];
        _arrayData = [[NSMutableArray alloc]initWithArray:[info objectForKey:@"list"]];

    }
    return self;
}

-(void)loadView
{
    [super loadView];
    
    _waterFlow = [[WaterFlowView alloc] initWithFrame:self.view.bounds];
    _waterFlow.waterFlowViewDelegate = self;
    _waterFlow.waterFlowViewDatasource = self;
    _waterFlow.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleTopMargin;
    _waterFlow.delegate = self;
    _waterFlow.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_waterFlow];    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setTitle:self.title showBackButton:YES];
    
    [_waterFlow reloadData];

}


#pragma mark WaterFlowViewDataSource
- (NSInteger)numberOfColumsInWaterFlowView:(WaterFlowView *)waterFlowView
{
    return 1;
}

- (NSInteger)numberOfAllWaterFlowView:(WaterFlowView *)waterFlowView{
    
    return [_arrayData count];
}

- (UIView *)waterFlowView:(WaterFlowView *)waterFlowView cellForRowAtIndexPath:(IndexPath *)indexPath
{
    ImageViewCell *view = [[ImageViewCell alloc] initWithIdentifier:nil];
    return view;
}

-(void)waterFlowView:(WaterFlowView *)waterFlowView  relayoutCellSubview:(UIView *)view withIndexPath:(IndexPath *)indexPath
{
    int arrIndex = indexPath.row * waterFlowView.columnCount + indexPath.column;
    NSDictionary *object = [_arrayData objectAtIndex:arrIndex];
    ImageViewCell *imageViewCell = (ImageViewCell *)view;
    imageViewCell.indexPath = indexPath;
    imageViewCell.columnCount = waterFlowView.columnCount;
    [imageViewCell relayoutViews];
    NSString *imageUrl = [object objectForKey:@"src"];
    if (imageUrl&&[imageUrl length]>0)
    {
        NSURL *URL = [NSURL URLWithString:imageUrl];
        [(ImageViewCell *)view setImageWithURL:URL placeholderImage:[UIImage imageNamed:@""] ];
    }
    else
    {
        [(ImageViewCell *)view setImageWithURL:nil placeholderImage:[UIImage imageNamed:@""]];
    }
}

#pragma mark WaterFlowViewDelegate
- (CGFloat)waterFlowView:(WaterFlowView *)waterFlowView heightForRowAtIndexPath:(IndexPath *)indexPath
{
    int arrIndex = indexPath.row * waterFlowView.columnCount + indexPath.column;
    NSDictionary *dict = [_arrayData objectAtIndex:arrIndex];
    float width = 0.0f;
    float height = 0.0f;
    if (dict)
    {
        NSObject *width_ = [dict objectForKey:@"width"];
        if (width_ && ![width_ isKindOfClass:[NSNull class]] && [width_ isKindOfClass:[NSString class]])
        {
            width = [(NSString*)width_ floatValue];
        }
        else
        {
            width = 100;
        }
        
        NSObject *height_ = [dict objectForKey:@"height"];
        if (height_ && ![height_ isKindOfClass:[NSNull class]] && [height_ isKindOfClass:[NSString class]])
        {
            height = [(NSString*)height_ floatValue];
        }
        else
        {
            height = 100;
        }
    }
    CGFloat new_height = waterFlowView.cellWidth * (height/width);
    HYLog(@"%f",new_height);
    return new_height;
}

- (void)waterFlowView:(WaterFlowView *)waterFlowView didSelectRowAtIndexPath:(IndexPath *)indexPath
{
    NSLog(@"indexpath row == %d,column == %d",indexPath.row,indexPath.column);
//    int arrIndex = indexPath.row * waterFlowView.columnCount + indexPath.column;
//    MSShowerDetailVC *vc = [[MSShowerDetailVC alloc]initWithDatas:_arrayData currentIndex:arrIndex];
//    [self.navigationController pushViewController:vc animated:YES];
}




- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
